import hashlib
import logging

logger = logging.getLogger("payment")


class Wechat_Base(object):
    @staticmethod
    def signature(noncestr, timestamp, url, jsapi_ticket):
        """
        计算jsapi的签名
        """
        params = dict(
            noncestr=noncestr,
            jsapi_ticket=jsapi_ticket,
            timestamp=timestamp,
            url=url
        )
        dics = sorted(params.items(), key=lambda asd: asd[0], reverse=False)
        str_a = ""
        for item in dics:
            k = item[0]
            v = item[1]
            str_a += k + "=" + str(v) + "&"
        str_a = str_a[0:-1]
        sha1_val = hashlib.sha1(str_a.encode()).hexdigest()
        return sha1_val
